package com.ztej.query.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.ztej.query.R;
import com.ztej.query.activity.MainActivity;
import com.ztej.query.activity.StaffActivity;
import com.ztej.query.activity.StaffInfoActivity;
import com.ztej.query.application.MyApplication;
import com.ztej.query.model.Contact;
import com.ztej.query.utils.Tools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.R.id.list;
import static java.security.AccessController.getContext;

/**
 * Created by mac_xhq on 2017/1/13.
 */

public class StaffAdapter extends BaseAdapter {

    private int selectedPosition = -1;

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    private Context context;
//    private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    private List<Contact> list = new ArrayList<Contact>();
    public StaffAdapter(Activity activity, List<Contact> list) {
        this.context = activity;
        this.list = list;
    }




    //返回的要显示的条目的数量
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    //返回一个View对象，会作为ListView的一个条目显示在界面上
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Map<String, String> map = new HashMap<String, String>();

        boolean isSelected = position == selectedPosition;

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.icon_stub)
                .showImageForEmptyUri(R.drawable.icon_empty)
                .showImageOnFail(R.drawable.icon_error)
                .cacheInMemory()
                .cacheOnDisc()
                .displayer(new RoundedBitmapDisplayer(5))
                .build();

        View v = null;
        ViewHolder mHolder = null;
        if(convertView == null){
            //如何填充的
            v = View.inflate(context, R.layout.item_lv_staff, null);
            //创建viewHoler封装所有条目使用的组件
            mHolder = new ViewHolder();
            mHolder.ivHead = (ImageView) v.findViewById(R.id.ivHead);
            mHolder.rlStaff = (RelativeLayout) v.findViewById(R.id.rlStaff);
            mHolder.tvDepartment = (TextView) v.findViewById(R.id.tvDepartment);
            mHolder.tvName = (TextView) v.findViewById(R.id.tvName);
            mHolder.tvPhone = (TextView) v.findViewById(R.id.tvPhone);
            //把viewHolder封装至view对象中，这样view被缓存时，viewHolder也就被缓存了
            v.setTag(mHolder);
        }
        else{
            v = convertView;
            //从view中取出保存的viewHolder，viewHolder中就有所有的组件对象，不需要再去findViewById
            mHolder = (ViewHolder) v.getTag();
        }

        //给条目中的每个组件设置要显示的内容
//        if (isSelected){
//            v.setBackgroundColor(parent.getResources().getColor(R.color.fly_bg));
//        }else {
////                v.setBackgroundColor(parent.getResources().getColor(R.color.white));
//            v.setBackground(getResources().getDrawable(R.drawable.bg_corner));
//
//        }


        ImageLoader.getInstance().displayImage(list.get(position).getPhotourl(), mHolder.ivHead, options);

//        mHolder.ivHead.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(context, StaffInfoActivity.class);
//                intent.putExtra("id", list.get(position).getId());
//                context.startActivity(intent);
//            }
//        });
        mHolder.rlStaff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, StaffInfoActivity.class);
                intent.putExtra("id", list.get(position).getId());
                context.startActivity(intent);
            }
        });
        mHolder.tvDepartment.setText(list.get(position).getRegion());
        mHolder.tvName.setText(list.get(position).getName());
        mHolder.tvPhone.setText(list.get(position).getPhone());
        mHolder.tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tools.call(context, list.get(position).getPhone());
            }
        });

        return v;
    }

    //把条目需要使用到的所有组件封装在这个类中
    class ViewHolder{
        RelativeLayout rlStaff;
        ImageView ivHead;
        TextView tvDepartment;
        TextView tvName;
        TextView tvPhone;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

}
